package com.p2p.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.p2p.model.entity.Account;
import com.p2p.model.entity.Bidrequest;
import com.p2p.model.entity.Bidrequestaudithistory;
import com.p2p.model.entity.Userinfo;
import com.p2p.model.vo.BidrequestQueryVo;
import com.p2p.system.common.Result;
import com.p2p.system.service.AccountService;
import com.p2p.system.service.BidrequestService;
import com.p2p.system.service.BidrequestaudithistoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 借款信息表 前端控制器
 * </p>
 *
 * @author 黄旭彬
 * @since 2023-12-12
 */
@CrossOrigin
@RestController
@Api(tags = "p2p项目审核模块")
@RequestMapping("/system/bidrequest")
@Configuration
public class BidrequestController {

    @Autowired
    private BidrequestService bidrequestService;

    @Autowired
    private BidrequestaudithistoryService bidrequestaudithistoryService;

    @Autowired
    private AccountService accountService;

    @ApiOperation("一审查询")
    @GetMapping("/{page}/{limit}")
    public Result getUserList(
            @PathVariable Long page,
            @PathVariable Long limit,
            BidrequestQueryVo BidrequestQueryVo

    ){
        Page<Bidrequest> pageParam = new Page<>(page,limit);
        IPage<Bidrequest> pageModel1= bidrequestService.selectPage(pageParam,BidrequestQueryVo);
        return Result.ok(pageModel1);
    }
    @ApiOperation("二审查询")
    @GetMapping("/two/{page}/{limit}")
    public Result getUserTwoList(
            @PathVariable Long page,
            @PathVariable Long limit,
            BidrequestQueryVo BidrequestQueryVo

    ){
        Page<Bidrequest> pageParam = new Page<>(page,limit);
        IPage<Bidrequest> pageModel1= bidrequestService.selectTwoPage(pageParam,BidrequestQueryVo);
        return Result.ok(pageModel1);
    }



    @ApiOperation("全部列表")
    @GetMapping("/all/{page}/{limit}")
    public Result getloanAllList(
            @PathVariable Long page,
            @PathVariable Long limit,
            BidrequestQueryVo BidrequestQueryVo

    ){
        Page<Bidrequest> pageParam = new Page<>(page,limit);
        IPage<Bidrequest> pageModel2= bidrequestService.selectAllPage(pageParam,BidrequestQueryVo);
        return Result.ok(pageModel2);
    }

    @ApiOperation("全部列表")
    @GetMapping("/publish/{page}/{limit}")
    public Result getloanpublishList(
            @PathVariable Long page,
            @PathVariable Long limit,
            BidrequestQueryVo BidrequestQueryVo

    ){
        Page<Bidrequest> pageParam = new Page<>(page,limit);
        IPage<Bidrequest> pageModel2= bidrequestService.selectpublishPage(pageParam,BidrequestQueryVo);
        return Result.ok(pageModel2);
    }




    @ApiOperation("根据id查找用户贷款申请表")
    @PostMapping("findLoanById/{id}")
    public Result findUserById(@PathVariable Long id){
        Bidrequest byId = this.bidrequestService.getById(id);
        if (byId!=null){
            return Result.ok(byId);
        }else {
            return Result.fail();
        }
    }

    @ApiOperation("申请请求")
    @PostMapping("applyNewBidQuest")
    public Result newBidQuest(@RequestBody Bidrequest bidrequest){
        // 设置申请时间为当前时间
        LocalDateTime currentDateTime = LocalDateTime.now();
        Timestamp timestamp = Timestamp.valueOf(currentDateTime);
        bidrequest.setApplytime(timestamp.toLocalDateTime());
//        设置截止日期 截至日期等于当前时间加上 期限：bidrequest.getMonthes2return() 乘以30天
        LocalDateTime disableDate = currentDateTime.plusMonths(bidrequest.getMonthes2return()).plusDays(bidrequest.getMonthes2return() * 30);
        bidrequest.setDisabledate(disableDate);
        System.out.println(bidrequest);

        boolean save = this.bidrequestService.save(bidrequest);
        if (save){
            return Result.ok();
        }else {
            return Result.fail();
        }

    }

    @ApiOperation("一审审核通过")
    @PostMapping("ApprovedById1")
    public Result ApprovedById1(@RequestBody Bidrequest bidrequest){
        bidrequest.setBidrequeststate(1);
        // 设置时间为当前时间
        LocalDateTime currentDateTime = LocalDateTime.now();
        Timestamp timestamp = Timestamp.valueOf(currentDateTime);

        bidrequest.setDisabledays(100);
        bidrequest.setPublishtime(timestamp.toLocalDateTime());
        boolean save = this.bidrequestService.updateById(bidrequest);
        if (save){
//  申请成功后将审核记录保存到审核历史表
            Bidrequestaudithistory bidReqH = new Bidrequestaudithistory();
            bidReqH.setState(bidrequest.getBidrequeststate());
            bidReqH.setRemark(bidrequest.getNote());
            bidReqH.setAudittime(bidrequest.getPublishtime());
            bidReqH.setApplytime(bidrequest.getApplytime());
            bidReqH.setAuditorId(1L);
            bidReqH.setApplierId(bidrequest.getCreateuserId());
            bidReqH.setBidrequestid(bidrequest.getId());
            bidReqH.setAudittype(bidrequest.getBidrequesttype());

            this.bidrequestaudithistoryService.save(bidReqH);
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    @ApiOperation("二审审核通过")
    @PostMapping("ApprovedById2")
    public Result ApprovedById2(@RequestBody Bidrequest bidrequest){
        bidrequest.setBidrequeststate(2);
        // 设置时间为当前时间
        LocalDateTime currentDateTime = LocalDateTime.now();
        Timestamp timestamp = Timestamp.valueOf(currentDateTime);
        bidrequest.setPublishtime(timestamp.toLocalDateTime());
        boolean save = this.bidrequestService.updateById(bidrequest);
        if (save){
//  申请成功后将审核记录保存到审核历史表
            Bidrequestaudithistory bidReqH = new Bidrequestaudithistory();
            bidReqH.setState(bidrequest.getBidrequeststate());
            bidReqH.setRemark(bidrequest.getNote());
            bidReqH.setAudittime(bidrequest.getPublishtime());
            bidReqH.setApplytime(bidrequest.getApplytime());
            bidReqH.setAuditorId(001L);
            bidReqH.setApplierId(bidrequest.getCreateuserId());
            bidReqH.setBidrequestid(bidrequest.getId());
            bidReqH.setAudittype(bidrequest.getBidrequesttype());

//   二审通过将添加第一期贷款到账户余额
            Account account = accountService.getById(bidrequest.getCreateuserId());

//            第一期贷款金额+余额
            BigDecimal bidrequestamount = bidrequest.getBidrequestamount();
//            BigDecimal divide = bidrequestamount.divide(BigDecimal.valueOf(bidrequest.getMonthes2return()),BigDecimal.ROUND_CEILING);
            BigDecimal usableamount = account.getUsableamount();
            account.setUsableamount(bidrequestamount.add(usableamount));

            accountService.updateById(account);

            this.bidrequestaudithistoryService.save(bidReqH);

            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    @ApiOperation("拒绝通过")
    @PostMapping("pass")
    public Result pass(@RequestBody Bidrequest bidrequest){
        bidrequest.setBidrequeststate(3);
        // 设置时间为当前时间
        LocalDateTime currentDateTime = LocalDateTime.now();
        Timestamp timestamp = Timestamp.valueOf(currentDateTime);
        bidrequest.setPublishtime(timestamp.toLocalDateTime());
        boolean save = this.bidrequestService.updateById(bidrequest);
        if (save){
//  申请成功后将审核记录保存到审核历史表
            Bidrequestaudithistory bidReqH = new Bidrequestaudithistory();
            bidReqH.setState(bidrequest.getBidrequeststate());
            bidReqH.setRemark(bidrequest.getNote());
            bidReqH.setAudittime(bidrequest.getPublishtime());
            bidReqH.setApplytime(bidrequest.getApplytime());
            bidReqH.setAuditorId(001L);
            bidReqH.setApplierId(bidrequest.getCreateuserId());
            bidReqH.setBidrequestid(bidrequest.getId());
            bidReqH.setAudittype(bidrequest.getBidrequesttype());

            this.bidrequestaudithistoryService.save(bidReqH);
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    @ApiOperation("投标操作")
    @PostMapping("/toubiao")
    public Result toubiao(@RequestBody Bidrequest bidrequest){

        boolean b = this.bidrequestService.updateById(bidrequest);
        if (b){
            return Result.ok();
        }else {
            return Result.fail();
        }

    }

//    根据id来查用户借款信息
    @ApiOperation("根据id来查借款信息")
    @PostMapping("/getuserBidById/{id}")
    public Result getuserBidById(@PathVariable Long id){
        Bidrequest bidrequest = bidrequestService.selectUserBidById(id);
        return Result.ok(bidrequest);
    }


}

